/**
 * 曾雪松
 * 24.12.25
 */
package org.qengine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.qengine.dao.StuInfoDao;
import org.qengine.entity.StuInfoEntity;
import org.qengine.service.PaperInfoService;
import org.qengine.service.StuInfoService;
import org.qengine.vo.StuInfoAnlysVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StuInfoServiceImpl extends ServiceImpl<StuInfoDao, StuInfoEntity>
        implements StuInfoService {

    @Autowired
    private PaperInfoService paperInfoService;

    @Autowired
    private StuInfoDao stuInfoDao;

    @Autowired
    private ObjectMapper objectMapper; // json 转换工具

    @Override
    public boolean updatePaperIDs(Long stuID, List<Long> paperIDs) {
        // 将 paperIDs 转换为 JSON 字符串
        String paperIDSJson;
        try {
            paperIDSJson = objectMapper.writeValueAsString(paperIDs);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON 转换失败", e);
        }

        // 更新 stuInfo 的 paperIDs 字段
        StuInfoEntity stuInfo = new StuInfoEntity();
        stuInfo.setStuID(stuID);
        stuInfo.setPaperIDs(paperIDSJson);

        // 使用 MyBatis-Plus 的 updateById 方法更新数据库
        return stuInfoDao.updateById(stuInfo) > 0;
    }

    @Override
    public List<Long> getStuPaperIDs(Long stuID) {
        // 使用 MyBatis-Plus 的 getById 方法获取学生信息
        StuInfoEntity stuInfo = stuInfoDao.selectById(stuID);
        if (stuInfo == null) {
            return null;
        }

        // 获取 paperIDs 字段的值
        String paperIDs = stuInfo.getPaperIDs();

        // 将 paperIDs 转换为 List<Long>
        List<Long> paperIDList;
        try {
            paperIDList = objectMapper.readValue(paperIDs, new TypeReference<List<Long>>() {});
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON 转换失败", e);
        }

        return paperIDList;
    }




    @Override
    public StuInfoEntity getStuInfo(Long stuID) {
        return stuInfoDao.getStuInfo(stuID);
    }


    @Override
    public StuInfoEntity updateStuInfo(StuInfoEntity stuInfoEntity) {
        // 使用 lambda 表达式简化 QueryWrapper 的创建
        QueryWrapper<StuInfoEntity> queryWrapper = new QueryWrapper<StuInfoEntity>()
                .eq("stuID", stuInfoEntity.getStuID());

        // 尝试更新现有记录
        int updatedRows = stuInfoDao.update(stuInfoEntity, queryWrapper);

        if (updatedRows == 0) {
            // 如果更新影响的行数为 0，说明记录不存在，执行插入
            stuInfoDao.insert(stuInfoEntity);
        }

        return stuInfoEntity;
    }


    @Override
    public int deleteStuInfo(Long stuID) {
        return stuInfoDao.deleteStuInfo(stuID);
    }

    @Override
    public List<StuInfoEntity> getAllStuInfo() {
        return stuInfoDao.getAllStuInfo();
    }
    // 根据学生姓名进行模糊查询
    public List<StuInfoEntity> getStuInfoByStuName(String stuName) {
        QueryWrapper<StuInfoEntity> queryWrapper = new QueryWrapper<StuInfoEntity>()
                .like("stuName", stuName);
        return stuInfoDao.selectList(queryWrapper);
    }

    // 根据学生姓名和学号进行模糊查询
    public List<StuInfoEntity> getStuInfoByStuNameAndStuID(String stuName, Long stuID) {
        QueryWrapper<StuInfoEntity> queryWrapper = new QueryWrapper<StuInfoEntity>()
                .like("stuName", stuName)
                .eq("stuID", stuID);
        return stuInfoDao.selectList(queryWrapper);
    }


}
